/**
 * 计算合并行
 * @param data 表格数据
 * @param key 区分行的Key
 * @param merge 合并列
 * @returns 计算后的表格数据
 */
export function mergeTableRow<T>(data: T[], key: string, merge: Array<string>) {
  if (!merge || merge.length === 0) {
    return data;
  }

  merge.forEach((m: string) => {
    const spanObj = {} as T;
    data = data.map((val, index) => {
      const rowVal = val[m];
      // 如果当前行的[key]与上一行相同，并且当前行的值和上一行的值相同，则合并
      if (spanObj[key] == val[key] && spanObj[rowVal]) {
        spanObj[rowVal]++;
        data[index - (spanObj[rowVal] - 1)][m + "-span"].rowspan++;
        val[m + "-span"] = {
          rowspan: 0,
          colspan: 0
        };
      } else {
        spanObj[key] = val[key];
        spanObj[rowVal] = 1;
        val[m + "-span"] = {
          rowspan: 1,
          colspan: 1
        };
      }
      return val;
    });
  });

  return data;
}

/**
 * el-table 合并单元格
 *
 * @param param 行列信息
 * @returns 合并单元格信息
 */
export function objectSpanMethod(data: {
  row: any;
  column: any;
  rowIndex: number;
  columnIndex: number;
}): number[] | { rowspan: number; colspan: number } | undefined {
  const span = data.column["property"] + "-span";
  if (data.row[span]) {
    return data.row[span];
  }
}
